using Ln.Fram.Ddd.Application;
using Ln.Fram.SqlSugarCore.Abstractions;
using Ln.Rbac.Application.Contracts.Dtos.OperLog;
using Ln.Rbac.Application.Contracts.IServices;
using Ln.Rbac.Domain.Operlog;
using SqlSugar;
using Volo.Abp.Application.Dtos;

namespace Ln.Rbac.Application.Services.RecordLog;

/// <summary>
/// OperationLog服务实现
/// </summary>
public class OperationLogService : LnCrudAppService<OperationLogEntity, OperationLogGetListOutputDto, Guid, OperationLogGetListInputVo>, IOperationLogService
{
	private ISqlSugarRepository<OperationLogEntity, Guid> _repository;

	public OperationLogService(ISqlSugarRepository<OperationLogEntity, Guid> repository) : base(repository)
	{
		_repository = repository;
	}

	public override async Task<PagedResultDto<OperationLogGetListOutputDto>> GetListAsync(OperationLogGetListInputVo input)
	{
		RefAsync<int> total = 0;
		//if (input.Sorting.IsNullOrWhiteSpace())
		//    input.Sorting = $"{nameof(OperationLogEntity.CreationTime)} Desc";
		var entities = await _repository._DbQueryable.WhereIF(!string.IsNullOrEmpty(input.OperUser), x => x.OperUser.Contains(input.OperUser!))
			.WhereIF(input.OperType is not null, x => x.OperType == input.OperType)
			.WhereIF(input.StartTime is not null && input.EndTime is not null, x => x.CreationTime >= input.StartTime && x.CreationTime <= input.EndTime)
			.OrderByDescending(it => it.CreationTime)
			.ToPageListAsync(input.SkipCount, input.MaxResultCount, total);
		return new PagedResultDto<OperationLogGetListOutputDto>(total, await MapToGetListOutputDtosAsync(entities));
	}

	[RemoteService(false)]
	public override Task<OperationLogGetListOutputDto> UpdateAsync(Guid id, OperationLogGetListOutputDto input)
	{
		return base.UpdateAsync(id, input);
	}
}
